package com.iwhalecloud.bss.kite.cucc.service.attr.handler;
import com.iwhalecloud.bss.kite.common.constant.SymbolConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class SelectUserFlagAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {
    @Autowired
    IOperateSceneInstService operateSceneInstService;
    static final String PHONE_NUM ="110008292" ;
    static final String IMEI="100000719" ;
    static final String IDFA="100000718" ;
    @Override
    public void set(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        //super.set(attr, inst, value, data);SelectUserFlagAttrHandler
//        String attrVal = attr.getValue();
//        if(KiteStringUtils.isNotEmpty(value)&&KiteStringUtils.isNotEmpty(attrVal)) {
//            String[] newVal = value.split(",");
//            if (value.contains(PHONENUM) && newVal.length > 1) {
//                //BssException.throwOut(KeyConsts.LOG_MODULE, "","不支持选择组合");
//                value = Arrays.stream(newVal).filter(nv -> !attrVal.contains(nv)).findFirst().isPresent()?Arrays.stream(newVal).filter(nv -> !attrVal.contains(nv)).findFirst().get():"";
//            }
//        }
        super.set(attr, inst, value, data);
    }
    @Override
    public void after(KiteAttr attr, KiteInst inst, AttrData data) {
        super.after(attr, inst, data);
        String attrVal = attr.getValue();
        if(attrVal.contains(PHONE_NUM)&&attrVal.contains(IDFA)||attrVal.contains(IMEI)){
            attrVal = Arrays.stream(attrVal.split(",")).filter(nv-> !PHONE_NUM.equals(nv)).collect(Collectors.joining(","));
        }
        attr.setValue(attrVal);
        operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), attr);
    }
    @Override
    public String getValueDesc(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        //return super.getValueDesc(attr, inst, value, data);
        List<KiteAttrValue> kiteAttrValues = attr.getAttrValues();
        String [] values = value.split(",");
        if(values.length <= 1 || KiteListUtils.isEmpty(kiteAttrValues)){
            return super.getValueDesc(attr, inst, value, data);
        }
        String ressf = "";
        for(String valueIt:values){
            String valueDesc = kiteAttrValues.stream().filter(attrValue -> KiteStringUtils.equals(attrValue.getAttrValue(), valueIt)).findFirst().map(KiteAttrValue::getAttrValueName).orElse(null);
            if(!KiteStringUtils.isAnyEmpty(valueDesc,ressf)){
                KiteStringUtils.concatWithSymbol(ressf,valueDesc, SymbolConsts.COMMA);
            }else {
                ressf = valueDesc;
            }
        }
        return ressf;
    }
}
